package com.example.dqdq.lzyim_asm_tvapp;

import android.content.Context;
import android.content.SharedPreferences;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.example.dqdq.lzyim_asm_tvapp.adapter.ExpandListViewAdapter;
import com.example.dqdq.lzyim_asm_tvapp.adapter.StaffListViewAdapter;
import com.example.dqdq.lzyim_asm_tvapp.api.API;
import com.example.dqdq.lzyim_asm_tvapp.bean.Node;
import com.example.dqdq.lzyim_asm_tvapp.bean.Problem;
import com.example.dqdq.lzyim_asm_tvapp.https.HttpManger;
import com.example.dqdq.lzyim_asm_tvapp.utils.ActivityCollector;
import com.umeng.analytics.MobclickAgent;


import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;


public class ServiceRecordActivity extends BaseActivity {

    private ListView mListView;
    private ExpandListViewAdapter mAdapter;

    private SharedPreferences preferences;
    private SharedPreferences.Editor editor;

     @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         ActivityCollector.addActivity(this);

         setContentView(R.layout.activity_service_record);

        initView(ServiceRecordActivity.this);

    }
    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }
    private void initView(final Context context) {
        mListView = (ListView) findViewById(R.id.mList);

        try {

            preferences = getSharedPreferences("user", Context.MODE_PRIVATE);
            final String str = "1";

            x.http().post(HttpManger.getParams(API.TV_WORKSHEELIST_URL,"clientid",String.valueOf(preferences.getInt("id",0))), new Callback.CommonCallback<String>() {
                @Override
                public void onSuccess(String result) {
                    List<Problem> ProblemList = new ArrayList<Problem>();
                    ProblemList.add(new Problem(1, "服务记录", 0));
                    try {
                        Log.e("test worklist", result);
                        JSONObject obj = new JSONObject(result);
                        JSONArray array = obj.getJSONArray("data");
                        for (int i = 0; i < array.length(); i++) {
                            ProblemList.add(new Problem(i + 2, array.getJSONObject(i).getString("ordernumber"), 1));
                        }
                        for (int i = 0; i < array.length(); i++) {
                            ProblemList.add(new Problem(i + array.length() + 2, array.getJSONObject(i).getString("address"), i + 2));

                        }

                        if (array.length()==0)
                        {
                            ProblemList.add(new Problem(2,"暂无服务记录", 1));

                        }



                        try {
                            mAdapter = new StaffListViewAdapter(mListView, context, ProblemList, 1);
                            mAdapter.setOnExpandNodeClickListener(new ExpandListViewAdapter.OnExpandNodeClickListener() {
                                @Override
                                public void onClick(Node node, List<Node> visibleNodes, View view, int position) {
                                    if (node.isLeaft()) {
                                        for (Node n : visibleNodes) {
                                            n.setSelected(false);
                                        }
                                        node.setSelected(true);
                                    }
                                    mAdapter.notifyDataSetChanged();
                                }
                            });
                            mListView.setAdapter(mAdapter);

                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {
                    //  Toast.makeText(x.app(), ex.getMessage(), Toast.LENGTH_LONG).show();
                }

                @Override
                public void onCancelled(Callback.CancelledException cex) {
                    //   Toast.makeText(x.app(), "cancelled", Toast.LENGTH_LONG).show();
                }

                @Override
                public void onFinished() {

                }
            });

        } catch (Exception e) {
            Toast.makeText(this, "实例化适配器失败", Toast.LENGTH_LONG).show();
        }

    }
    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        ActivityCollector.removeActivity(this);
    }


}
